package view;

import controllers.LoginInterfaceControllers;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * @author zjw
 * @date 2020/1/4 9:46
 */
public class LoginInterface extends Application {
    private ImageView logoImageView;
    private ImageView headPortraitImageView;
    private TextField textField;
    private PasswordField passwordField;
    private Hyperlink registerHyperlink;
    private Hyperlink forgetHyperlink;
    private Button loginButton;
    private AnchorPane anchorPane;

    /**
     * 各组件实例化
     */
    public LoginInterface() {
        // logo图片
        Image logoImage = new Image(getClass().getResourceAsStream("../image/loginBG.jpg"));
        logoImageView = new ImageView(logoImage);
        // 头像
        Image headPortraitImage = new Image(getClass().getResourceAsStream("../image/2.png"));
        headPortraitImageView = new ImageView(headPortraitImage);
        // 账号输入框
        textField = new TextField();
        // 密码输入框
        passwordField = new PasswordField();
        // 固定窗格
        anchorPane = new AnchorPane();
    }

    @Override
    public void start(Stage primaryStage) {
        // 通过该对象获得按钮组件的控制方法
        LoginInterfaceControllers loginInterfaceButtons = new LoginInterfaceControllers(primaryStage, textField, passwordField);
        // 注册账号
        registerHyperlink = loginInterfaceButtons.getRegisterHyperlink();
        // 忘记密码
        forgetHyperlink = new Hyperlink("忘记密码");
        // 登录按钮
        loginButton = loginInterfaceButtons.getLoginButton();

        // 初始化各组件大小并设置各组件在anchorPane布局上的位置位置
        this.initInterface();

        Scene scene = new Scene(anchorPane, 430, 330);
        // 加载css中设计的样式
        scene.getStylesheets().add(LoginInterface.class.getResource("css/LoginCss.css").toExternalForm());

        primaryStage.setScene(scene);
        primaryStage.setTitle("Login");
        // 白色背景，带有最小化/最大化/关闭等有操作系统平台装饰（默认状态）
        primaryStage.initStyle(StageStyle.DECORATED);
        primaryStage.getIcons().add(new Image(getClass().getResource("../image/logo.png").toExternalForm()));
        primaryStage.show();
    }

    /**
     * 初始化界面各组件位置信息，并将各组件放进anchorPane中
     */
    private void initInterface() {
        logoImageView.setSmooth(true);
        logoImageView.setFitWidth(430);
        logoImageView.setFitHeight(175);

        headPortraitImageView.setSmooth(true);
        headPortraitImageView.setLayoutX(35);
        headPortraitImageView.setLayoutY(195);
        headPortraitImageView.setFitHeight(70);
        headPortraitImageView.setFitWidth(70);

        textField.setLayoutX(125);
        textField.setLayoutY(195);
        textField.setPrefHeight(30);
        textField.setPrefWidth(190);

        passwordField.setLayoutX(125);
        passwordField.setLayoutY(227);
        passwordField.setPrefHeight(30);
        passwordField.setPrefWidth(190);

        registerHyperlink.setText("注册账号");
        registerHyperlink.setLayoutX(325);
        registerHyperlink.setLayoutY(198);

        forgetHyperlink.setLayoutX(325);
        forgetHyperlink.setLayoutY(230);

        loginButton.setText("登录");
        loginButton.setLayoutX(125);
        loginButton.setLayoutY(270);
        loginButton.setPrefHeight(30);
        loginButton.setPrefWidth(190);

        anchorPane.setStyle("-fx-background-color: #ffffff");
        anchorPane.getChildren().addAll(textField, passwordField,
                registerHyperlink, forgetHyperlink,
                logoImageView, headPortraitImageView,
                loginButton);
    }

}